package middleware

import (
	"run_order/core"
	"run_order/result"

	"github.com/gin-gonic/gin"
)

func AuthMiddleware(c *gin.Context) {
	tokenStr := c.GetHeader("token")
	tokenObj, tokenErr := core.ParseToken(tokenStr)
	if tokenStr == "" || tokenErr != nil {
		result.Failed(c, 401, "token无效")
		//在中间件中终止后面的程序执行,但是本函数内剩余的代码会执行,所以要return
		c.Abort()
		return
	}
	c.Set("tokenObj", tokenObj)
	c.Next()
}
func WxAuthMiddleware(c *gin.Context) {
	tokenStr := c.GetHeader("token")
	tokenObj, tokenErr := core.ParseWxToken(tokenStr)
	if tokenStr == "" || tokenErr != nil {
		result.Failed(c, 401, "微信解析token无效")
		//在中间件中终止后面的程序执行,但是本函数内剩余的代码会执行,所以要return
		c.Abort()
		return
	}
	c.Set("openId", tokenObj.OpenId)
	c.Set("phone", tokenObj.Phone)
	c.Next()
}
